/**
 * 
 */
package com.platform.common.utils.log;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;

import com.platform.common.utils.ReflectionUtils;

/**
 * 功能：
 * 
 * @author gaowei enterprise application platform version 0.1
 */
public class LoggerFactory {
	
	private static Map<Class<?>, Logger> cached = null;
	private static LoggerFactory instance = new LoggerFactory();
	
	private LoggerFactory() {
		cached = Collections.synchronizedMap(new HashMap<Class<?>, Logger>());
	}
	
	public static LoggerFactory get() {
		return instance;
	}
	
	/**
	 * <pre>
	 * 功能：
	 * </pre>
	 * 
	 * @param clazz
	 * @return
	 */
	private synchronized Logger getLogger(Class<?> clazz) {
		if (!cached.containsKey(clazz)) {
			Logger logger = org.slf4j.LoggerFactory.getLogger(clazz);
			cached.put(clazz, logger);
		}
		return cached.get(clazz);
	}
	
	/**
	 * <pre>
	 * 功能：
	 * </pre>
	 * 
	 * @param clazz
	 * @param text
	 * @param arguments
	 */
	public void info(Class<?> clazz, String text, Object... arguments) {
		getLogger(clazz).info(text);
	}
	
	public void info(String text, Object... arguments) {
		try {
			Class<?> clazz = ReflectionUtils.getInvokeClass();
			info(clazz, text, arguments);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * <pre>
	 * 功能：
	 * </pre>
	 * 
	 * @param clazz
	 * @param text
	 * @param arguments
	 */
	public void debug(Class<?> clazz, String text, Object... arguments) {
		getLogger(clazz).debug(text);
	}
	
	public void debug(String text, Object... arguments) {
		try {
			Class<?> clazz = ReflectionUtils.getInvokeClass();
			debug(clazz, text, arguments);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * <pre>
	 * 功能：
	 * </pre>
	 * 
	 * @param clazz
	 * @param text
	 * @param arguments
	 */
	public void warn(Class<?> clazz, String text, Object... arguments) {
		getLogger(clazz).warn(text);
	}
	
	public void warn(String text, Object... arguments) {
		try {
			Class<?> clazz = ReflectionUtils.getInvokeClass();
			warn(clazz, text, arguments);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * <pre>
	 * 功能：
	 * </pre>
	 * 
	 * @param clazz
	 * @param text
	 * @param arguments
	 */
	public void error(Class<?> clazz, String text, Object... arguments) {
		getLogger(clazz).error(text, arguments);
	}
	
	public void error(String text, Object... arguments) {
		try {
			Class<?> clazz = ReflectionUtils.getInvokeClass();
			error(clazz, text, arguments);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
}
